package org.footito.android.activity;

import java.util.ArrayList;

import org.footito.android.R;
import org.footito.android.adapter.CommentsBaseAdapter;
import org.footito.android.data.Event;
import org.footito.android.data.Group;
import org.footito.android.data.Subscriber;
import org.footito.android.data.TeamStatus;
import org.footito.android.data.database.ProviderAdapter;
import org.footito.android.tools.ConvertingTools;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class EventInfoActivity extends ListActivity {

	private static final String EVENT_ID = "eventid";
	private ProviderAdapter providerAdapter;
	private Event event;

	private String joinGroup;
	private String joinBy;
	private String commentedBy;

	private TextView eventcontentTV;
	private TextView teamName;
	private ImageView teamFlag;
	private TextView eventreactionsTV;
	private TextView timestamp;
	private TextView permanentLink;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_event_info);

		providerAdapter = new ProviderAdapter(this);
		eventcontentTV = (TextView) findViewById(R.id.eventText);
		eventreactionsTV = (TextView) findViewById(R.id.reactionsText);
		teamFlag = (ImageView) findViewById(R.id.blason);
		teamName = (TextView) findViewById(R.id.teamName);
		((Button) findViewById(R.id.refreshIV)).setVisibility(View.GONE);
		timestamp = (TextView) findViewById(R.id.TV_date);
		permanentLink = (TextView) findViewById(R.id.tv_permanentLink);

		joinGroup = getResources().getString(R.string.joinGroup);
		joinBy = getResources().getString(R.string.joinBy);
		commentedBy = getResources().getString(R.string.commentedBy);

		permanentLink.setMovementMethod(LinkMovementMethod.getInstance());
	}

	@Override
	protected void onResume() {
		super.onResume();

		int eventId = getIntent().getExtras().getInt(EVENT_ID);
		event = providerAdapter.getEvent(eventId);
		teamFlag.setImageBitmap(event.getTeam().getPicture(this));
		teamName.setText(event.getTeam().getName());
		timestamp.setText(ConvertingTools.getFormattedDate(event.getTimestamp()));

		if (event instanceof Group) {
			eventcontentTV.setText(Html.fromHtml("<b>" + joinGroup + " : </b> \""
					+ event.getContent() + "\""));
			eventreactionsTV.setText(joinBy);

			ArrayList<Subscriber> subs = ((Group) event).getSubscribers();

			if (subs.size() > 0) {
				String[] teams = new String[subs.size()];
				for (int i = 0; i < subs.size(); i++) {
					teams[i] = subs.get(i).getTeam().getName();
				}
				getListView()
						.setAdapter(
								new ArrayAdapter<String>(this, R.layout.row_sub,
										R.id.TV_Subscriber, teams));
			} else {
				String[] message = { getResources().getString(R.string.no_subscriber) };
				getListView().setAdapter(
						new ArrayAdapter<String>(this, R.layout.row_sub, R.id.TV_Subscriber,
								message));
			}
		} else {
			TeamStatus status = (TeamStatus) event;
			eventcontentTV.setText(status.getContent());
			setListAdapter(new CommentsBaseAdapter(this, status.getComments()));
			eventreactionsTV.setText(commentedBy);
			permanentLink.setText(Html.fromHtml("<a href=\"" + event.getLink()
					+ "\">Lien permanent</a>"));
		}
		// <a href="http://www.footito.fr/event/35130">Lien permanent</a>
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		super.onNewIntent(intent);
	}

	public static void launchActivity(Context context, Event event) {
		Intent intent = new Intent(context, EventInfoActivity.class);
		intent.putExtra(EVENT_ID, event.getId());
		context.startActivity(intent);
	}
}
